package com.ss.android.download;

import android.annotation.SuppressLint;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.database.Cursor;
import android.net.Uri;
import android.os.Build;
import android.os.SystemClock;
import android.text.TextUtils;
import android.util.Log;
import android.widget.RemoteViews;

import com.bytedance.common.utility.Logger;
import com.ss.android.article.browser.R;
import com.ss.android.common.download.DownloadInfoChangeListener;
import com.ss.android.common.download.DownloadShortInfo;
import com.ss.android.common.download.NotificationCompatBuilder;
import com.ss.android.download.a.c;

import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

@SuppressLint(value = {"UseSparseArrays"})
public final class g {
    static final Object d;
    private static g g;

    static {
        d = new Object();
    }

    private final Context e;
    private final NotificationManager f;
    private final Set h;
    private final HashMap i;
    private final c j;
    private final c k;
    Map a;
    Map b;
    Map c;

    private g(Context arg3) {
        this.a = new ConcurrentHashMap();
        this.b = new ConcurrentHashMap();
        this.c = new ConcurrentHashMap();
        this.h = new HashSet();
        this.i = new HashMap();
        this.j = new c();
        this.k = new c();
        this.e = arg3.getApplicationContext();
        this.f = (NotificationManager) this.e.getSystemService(Context.NOTIFICATION_SERVICE);
        try {
            com.ss.android.download.h.a(this.e, new h.a(this));
        } catch (Exception v0) {
        }
    }

    public static g a(Context arg2) {
        g v0_1;
        Class v1 = g.class;
        synchronized (v1) {
            if (g == null) {
                g = new g(arg2);
            }
            v0_1 = g;
        }
        return v0_1;
    }

    public static int a(Cursor arg1, String arg2) {
        return arg1.getInt(arg1.getColumnIndexOrThrow(arg2));
    }

    static Set a(g arg1) {
        return arg1.h;
    }

    private static boolean a(int arg2) {
        boolean v0 = true;
        if (arg2 != 1 && arg2 != 3) {
            v0 = false;
        }

        return v0;
    }

    private static boolean a(int arg1, int arg2) {
        boolean v0;
        if (arg1 != 199 && arg1 != 198) {
            v0 = false;
        } else if (g.a(arg2)) {
            v0 = true;
        } else {
            label_8:
            v0 = false;
        }

        return v0;
    }

    static String b(DownloadInfo arg4) {
        String v0_1;
        int v2;
        int v0 = 1;
        if (arg4.j != 196 && arg4.j != 193 && arg4.j != 194 && arg4.j != 195) {
            v2 = 0;
        } else if (g.b(arg4.h)) {
            v2 = 1;
        } else {
            label_26:
            v2 = 0;
        }

        if (v2 != 0) {
            v0_1 = "2:" + arg4.a;
        } else {
            if (arg4.j != 192 || !g.b(arg4.h)) {
                v0 = 0;
            }

            if (v0 != 0) {
                return "1:" + arg4.a;
            }

            if (!g.c(arg4) && !g.b(arg4.j, arg4.h)) {
                return null;
            }

            v0_1 = "3:" + arg4.a;
        }

        return v0_1;
    }

    private static boolean b(int arg1) {
        boolean v0 = true;
        if (arg1 != 1 && arg1 != 0) {
            v0 = false;
        }

        return v0;
    }

    private static boolean b(int arg1, int arg2) {
        boolean v0 = !com.ss.android.download.a.a.c(arg1) || !g.a(arg2) ? false : true;
        return v0;
    }

    private static boolean c(DownloadInfo arg2) {
        return g.a(arg2.j, arg2.h);
    }

    public final void a(Context arg7, long arg8) {
        String[] v2 = null;
        Cursor v1 = com.ss.android.download.k.a(arg7).a(ContentUris.withAppendedId(com.ss.android.download.a.a.a, arg8), v2, ((String) null), v2, ((String) null));
        try {
            if (!v1.moveToFirst()) {
                Log.w("DownloadNotifier", "Missing details for download " + arg8);
                if (v1 != null) {
                    try {
                        v1.close();
                    } catch (Exception v0) {
                    }
                }
                return;
            }

            int v0_2 = a(v1, "status");
            int v3 = g.a(v1, "visibility");
            if (v1 != null) {
                try {
                    v1.close();
                } catch (Exception v1_1) {
                }
            }
            if ((g.b(v0_2, v3)) || (g.a(v0_2, v3))) {
                ContentValues v1_2 = new ContentValues();
                v1_2.put("visibility", Integer.valueOf(200));
                v1_2.put("visibility", Integer.valueOf(0));
                com.ss.android.download.k.a(arg7).a(ContentUris.withAppendedId(com.ss.android.download.a.a.a, arg8), v1_2, ((String) null), v2);
            }

            if (!g.b(v0_2, v3)) {
                return;
            }

            this.a("3:" + arg8);
            return;
        } catch (Exception v0) {
            v0.printStackTrace();
        }
    }

    public final void a(String arg3) {
        if (!TextUtils.isEmpty(arg3)) {
            this.f.cancel(arg3, 0);
            Object v1 = g.d;
            synchronized (v1) {
                if (this.h.contains(arg3)) {
                    this.h.remove(arg3);
                    this.c();
                }
            }
        }
    }

    public final boolean a(long arg4) {
        boolean v0_1;
        Object v0 = this.b.get(Long.valueOf(arg4));
        if (android.support.design.a.isTextEmpty(((String) v0))) {
            v0_1 = false;
        } else {
            String[] v0_2 = ((String) v0).split("##");
            if (v0_2 != null && v0_2.length > 0 && ("bind_app".equals(v0_2[0]))) {
                return true;
            }

            v0_1 = false;
        }

        return v0_1;
    }

    public final void a(DownloadInfo arg5) {
        if ((arg5.v) && (this.a(arg5.a))) {
            arg5.j = 490;
            this.a(arg5, 3, 0);
        }
    }

    public final void a(Collection arg29) {
        Object v4_5;
        Notification v4_4;
        String v3_2;
        String v5_3;
        String v6_3;
        RemoteViews v7;
        long v20_1;
        c v20;
        long v6_1;
        long v8;
        Intent v19;
        int v10;
        int v4_2;
        long v4_1;
        long v12;
        NotificationCompatBuilder v18;
        Object v3_1;
        String v4;
        Object v2_1;
        Iterator v3;
        Resources v14;
        HashMap v11 = this.i;
        synchronized (v11) {
            try {
                if (!android.support.design.a.p()) {
                    return;
                }
                v14 = this.e.getResources();
                HashMap v15 = new HashMap();
                v3 = arg29.iterator();
                while (v3.hasNext()) {
                    v2_1 = v3.next();
                    v4 = g.b(((DownloadInfo) v2_1));
                    if (v4 != null) {
                        ((Map) v15).put(v4, v2_1);
                        continue;
                    }

                    if (!com.ss.android.download.a.a.c(((DownloadInfo) v2_1).j)) {
                        continue;
                    }

                    this.a(((DownloadInfo) v2_1), 3, 0);
                }

                Iterator v16 = ((Map) v15).keySet().iterator();
                while (true) {
                    label_33:
                    if (!v16.hasNext()) {
                        v3 = this.i.keySet().iterator();
                        while (true) {
                            label_518:
                            if (!v3.hasNext()) {
                                return;
                            }
                            v2_1 = v3.next();
                            if (((Map) v15).containsKey(v2_1)) {
                                continue;
                            }
                            this.f.cancel(((String) v2_1), 0);
                            v4_5 = g.d;
                            synchronized (v4_5) {
                                if (this.h.contains(v2_1)) {
                                    this.h.remove(v2_1);
                                    this.c();
                                }
                            }
                            v3.remove();
                            continue;
                        }
                    }
                    v2_1 = v16.next();
                    int v17 = Integer.parseInt(((String) v2_1).substring(0, ((String) v2_1).indexOf(58)));
                    v3_1 = ((Map) v15).get(v2_1);
                    v18 = android.support.design.a.o().d(this.e);
                    if (this.i.containsKey(v2_1)) {
                        v12 = ((Long) this.i.get(v2_1)).longValue();
                    } else {
                        v4_1 = System.currentTimeMillis();
                        this.i.put(v2_1, Long.valueOf(v4_1));
                        v12 = v4_1;
                    }

                    v4_2 = 0;
                    if (v17 == 1) {
                        v10 = 17301633;
                    } else if (v17 == 2) {
                        this.a(((DownloadInfo) v3_1), 2, 0);
                        v10 = 17301642;
                    } else {
                        if (v17 == 3) {
                            v4_2 = 17301634;
                            this.a(((DownloadInfo) v3_1), 3, 0);
                        }

                        v10 = v4_2;
                    }

                    if (v17 == 1 || v17 == 2) {
                        v18.setContentIntent(PendingIntent.getService(this.e, 0,
                                new Intent("android.ss.intent.action.DOWNLOAD_DELETE",
                                        ContentUris.withAppendedId(com.ss.android.download.a.a.a, ((DownloadInfo) v3_1).a),
                                        this.e, DownloadHandlerService.class), PendingIntent.FLAG_UPDATE_CURRENT));
                        if (v17 == 1) {
                            if (v18.builder == null) {
//                            goto label_93;
                            } else {
                                v18.builder.setOngoing(true);
//                            goto label_93;
                            }
                        } else {
                            v18.setAutoCancel();
                        }
                    } else if (v17 == 3) {
                        Uri v5 = ContentUris.withAppendedId(com.ss.android.download.a.a.a, ((DownloadInfo) v3_1).a);
                        v18.setAutoCancel();
                        v4 = (com.ss.android.download.a.a.b(((DownloadInfo) v3_1).j)) || (g.c(((DownloadInfo) v3_1))) ? "android.ss.intent.action.DOWNLOAD_DELETE" : "android.ss.intent.action.DOWNLOAD_OPEN";
                        Intent v6 = new Intent(v4, v5, this.e, DownloadHandlerService.class);
                        v6.putExtra("extra_click_download_ids", ((DownloadInfo) v3_1).a);
                        v18.setContentIntent(PendingIntent.getService(this.e, 0, v6, PendingIntent.FLAG_UPDATE_CURRENT));
                        PendingIntent v4_3 = PendingIntent.getService(this.e, 0, new Intent("android.ss.intent.action.DOWNLOAD_HIDE", v5, this.e, DownloadHandlerService.class), 0);
                        if (v18.builder == null) {
//                        goto label_93;
                        } else {
                            v18.builder.setDeleteIntent(v4_3);
                        }
                    }

//                label_93:
                    v4_2 = 0;
                    v19 = new Intent("android.ss.intent.action.DOWNLOAD_CLICK", ContentUris.withAppendedId(com.ss.android.download.a.a.a, ((DownloadInfo) v3_1).a), this.e, DownloadHandlerService.class);
                    v19.putExtra("extra_click_download_ids", ((DownloadInfo) v3_1).a);
                    v19.putExtra("extra_notification_tag", ((String) v2_1));
                    if (v17 == 1) {
                        v8 = 0;
                        v6_1 = 0;
                        v4_1 = 0;
                        v20 = this.j;
                        synchronized (v20) {
                            if (((DownloadInfo) v3_1).r != -1) {
                                v8 = ((DownloadInfo) v3_1).s;
                                v4_1 = ((DownloadInfo) v3_1).r;
                                v6_1 = this.j.a(((DownloadInfo) v3_1).a);
                            } else {
                                long v26 = v4_1;
                                v4_1 = v6_1;
                                v6_1 = v26;
                            }
                        }
                        v20_1 = 0;
                        if (v4_1 > v20_1) {
                            v8 *= 100;
                            v4_2 = ((int) (v8 / v4_1));
                        } else {
                            v4_2 = 0;
                        }
                        this.a(((DownloadInfo) v3_1), 1, v6_1);
                    }
                    if (v18.builder != null) {
                        v18.builder.setWhen(v12);
                    }

                    if (v18.builder != null) {
                        v18.builder.setSmallIcon(v10);
                    }

                    v7 = new RemoteViews(this.e.getPackageName(), R.layout.cv);
                    if (Build.VERSION.SDK_INT <= 20) {
//                goto label_187;
                    } else if (!android.support.design.a.o().e(this.e)) {
//                goto label_187;
                    } else {
                        v7.setInt(com.ss.android.article.browser.R.id.root, "setBackgroundColor", this.e.getResources().getColor(com.ss.android.article.browser.R.color.nz));
                    }

                    int v5_2 = R.id.download_progress;
                    int v6_2 = 100;
                    try {
                        v7.setProgressBar(v5_2, v6_2, v4_2, false);
                        v7.setImageViewResource(com.ss.android.article.browser.R.id.icon, v10);
                        v7.setOnClickPendingIntent(com.ss.android.article.browser.R.id.s2, PendingIntent.getService(this.e, 0, v19, PendingIntent.FLAG_UPDATE_CURRENT));
                        v5_2 = R.id.desc;
                        v4 = !TextUtils.isEmpty(((DownloadInfo) v3_1).z) ? ((DownloadInfo) v3_1).z : v14.getString(com.ss.android.article.browser.R.string.j9);
                        v7.setTextViewText(v5_2, v4);
                        v6_3 = "";
                        v5_3 = "";
                        v4 = "";
                        if (v17 == 1) {
                            v6_3 = android.support.design.a.a(((DownloadInfo) v3_1).s) + "/" + android.support.design.a.a(((DownloadInfo) v3_1).r);
                            v5_3 = this.e.getResources().getString(com.ss.android.article.browser.R.string.notification_downloading);
                            v4 = this.e.getResources().getString(com.ss.android.article.browser.R.string.notification_download_pause);
                            v7.setViewVisibility(com.ss.android.article.browser.R.id.download_progress, 0);
                            v7.setViewVisibility(com.ss.android.article.browser.R.id.download_success, 8);
                            v7.setViewVisibility(com.ss.android.article.browser.R.id.download_text, 0);
                            if (this.a(((DownloadInfo) v3_1).a)) {
                                v7.setViewVisibility(com.ss.android.article.browser.R.id.s2, 8);
                                v3_2 = v4;
                                v4 = v5_3;
                                v5_3 = v6_3;
                            } else {
                                v7.setViewVisibility(com.ss.android.article.browser.R.id.s2, 0);
                                v3_2 = v4;
                                v4 = v5_3;
                                v5_3 = v6_3;
                            }
                        } else if (v17 == 2) {
                            v6_3 = android.support.design.a.a(((DownloadInfo) v3_1).s) + "/" + android.support.design.a.a(((DownloadInfo) v3_1).r);
                            v5_3 = this.e.getResources().getString(com.ss.android.article.browser.R.string.notification_download_pause);
                            v4 = this.e.getResources().getString(com.ss.android.article.browser.R.string.notification_download_resume);
                            v7.setViewVisibility(com.ss.android.article.browser.R.id.download_progress, 8);
                            v7.setViewVisibility(com.ss.android.article.browser.R.id.download_success, 8);
                            v7.setViewVisibility(com.ss.android.article.browser.R.id.download_text, 0);
                            if (this.a(((DownloadInfo) v3_1).a)) {
                                v7.setViewVisibility(com.ss.android.article.browser.R.id.s2, 8);
                                v3_2 = v4;
                                v4 = v5_3;
                                v5_3 = v6_3;
                            } else {
                                v7.setViewVisibility(com.ss.android.article.browser.R.id.s2, 0);
                                v3_2 = v4;
                                v4 = v5_3;
                                v5_3 = v6_3;
                            }
                        } else if (v17 == 3) {
                            if (com.ss.android.download.a.a.b(((DownloadInfo) v3_1).j) || g.c(((DownloadInfo) v3_1))) {
                                v5_3 = "";
                                v7.setViewVisibility(com.ss.android.article.browser.R.id.download_success_size, 8);
                                v4 = g.c(((DownloadInfo) v3_1)) ? this.e.getResources().getString(com.ss.android.article.browser.R.string.notification_download_space_failed) : this.e.getResources().getString(com.ss.android.article.browser.R.string.notification_download_failed);
                                v3_2 = this.e.getResources().getString(com.ss.android.article.browser.R.string.notification_download_restart);
                            } else if (com.ss.android.download.a.a.a(((DownloadInfo) v3_1).j)) {
                                v5_3 = android.support.design.a.a(((DownloadInfo) v3_1).r);
                                v4 = android.support.design.a.o().a(this.e, ((DownloadInfo) v3_1).e) ? this.e.getResources().getString(com.ss.android.article.browser.R.string.notification_download_complete_open) : this.e.getResources().getString(com.ss.android.article.browser.R.string.notification_download_complete);
                                v3_2 = this.e.getResources().getString(com.ss.android.article.browser.R.string.notification_download_install);
                            } else {
                                v3_2 = v4;
                                v4 = v5_3;
                                v5_3 = v6_3;
                            }
                            v7.setViewVisibility(com.ss.android.article.browser.R.id.download_progress, 8);
                            v7.setViewVisibility(com.ss.android.article.browser.R.id.download_success, 0);
                            v7.setViewVisibility(com.ss.android.article.browser.R.id.download_text, 8);
                            v7.setViewVisibility(com.ss.android.article.browser.R.id.s2, 8);
                        } else {
                            v3_2 = v4;
                            v4 = v5_3;
                            v5_3 = v6_3;
                        }
                        v7.setTextViewText(com.ss.android.article.browser.R.id.download_size, ((CharSequence) v5_3));
                        v7.setTextViewText(com.ss.android.article.browser.R.id.download_status, ((CharSequence) v4));
                        v7.setTextViewText(com.ss.android.article.browser.R.id.download_success_size, ((CharSequence) v5_3));
                        v7.setTextViewText(com.ss.android.article.browser.R.id.s1, ((CharSequence) v4));
                        v7.setTextViewText(com.ss.android.article.browser.R.id.s2, ((CharSequence) v3_2));
                        v4_4 = null;
                        if (v18.builder != null) {
                            v4_4 = v18.builder.build();
                        } else {
                            v4_4 = null;
                        }
                        v4_4.contentView = v7;
                        v3_1 = g.d;
                        synchronized (v3_1) {
                            if (!this.h.contains(v2_1)) {
                                this.h.add(v2_1);
                                this.c();
                            }
                        }
                        if (v4_4 == null) {
                        } else {
                            this.f.notify(((String) v2_1), 0, v4_4);
                        }
                        continue;
                    } catch (Throwable v3_3) {
                        try {
                            label_509:
                            Logger.e("DownloadNotifier", "Failed to build Notification.", v3_3);
                        } catch (Throwable v2) {
                        }
                    }
                }
            } catch (Throwable v2) {
                v2.printStackTrace();
            }
        }
    }

    public final void a() {
        Object v1 = g.d;
        synchronized (v1) {
            Iterator v2 = this.h.iterator();
            while (v2.hasNext()) {
                this.f.cancel((String) v2.next(), 0);
                v2.remove();
            }
            return;
        }
    }

    private void a(DownloadInfo arg12, int arg13, long arg14) {
        Object v2_1;
        if (this.a.get(Long.valueOf(arg12.a)) != null) {
            Object v0 = this.a.get(Long.valueOf(arg12.a));
            v2_1 = this.c.get(Long.valueOf(arg12.a));
            if (v2_1 == null) {
                v2_1 = new DownloadShortInfo();
                this.c.put(Long.valueOf(arg12.a), v2_1);
            }

            ((DownloadShortInfo) v2_1).id = arg12.a;
            ((DownloadShortInfo) v2_1).b = com.ss.android.download.e.a(arg12.j);
            ((DownloadShortInfo) v2_1).c = arg12.r;
            ((DownloadShortInfo) v2_1).d = arg12.s;
            ((DownloadShortInfo) v2_1).fileName = arg12.e;
            if (v0 == null) {
                return;
            }

            try {
                if (((Map) v0).isEmpty()) {
                    return;
                }

                Iterator v0_2 = ((Map) v0).keySet().iterator();
                while (v0_2.hasNext()) {
                    Object v1 = v0_2.next();
                    if (v1 == null) {
                        continue;
                    }

                    ((DownloadInfoChangeListener) v1).onDownloadProgressChanged(((DownloadShortInfo) v2_1), arg13, arg12.r, arg12.s, arg14);
                }
            } catch (Throwable v0_1) {
            }
        }
    }

    public final void a(long arg6, long arg8) {
        c v1 = this.j;
        synchronized (v1) {
            if (arg8 != 0) {
                this.j.a(arg6, arg8);
                this.k.a(arg6, SystemClock.elapsedRealtime());
                return;
            } else {
                this.j.b(arg6);
                this.k.b(arg6);
                return;
            }
        }
    }

    public final void b() {
        c v1 = this.j;
        synchronized (v1) {
            int v0 = 0;
            while (v0 < this.j.c) {
                Log.d("DownloadNotifier", "Download " + this.j.a[v0] + " speed " + this.j.b[v0] + "bps, " + (SystemClock.elapsedRealtime() - this.k.a(this.j.a[v0])) + "ms ago");
                ++v0;
            }
            return;
        }
    }

    private void c() {
        Object v3;
        StringBuilder v2;
        try {
            v2 = new StringBuilder();
            v3 = g.d;
            synchronized (v3) {
                Iterator v4 = this.h.iterator();
                int v1;
                for (v1 = 0; v4.hasNext(); ++v1) {
                    Object v0_2 = v4.next();
                    if (v1 != this.h.size() - 1) {
                        v2.append(((String) v0_2)).append("|");
                    } else {
                        v2.append(((String) v0_2));
                    }
                }
            }
            com.ss.android.download.h.a(this.e, new com.ss.android.download.h.b(v2.toString()));
        } catch (Exception v0) {
        }
    }
}

